<template>
  <div>
    <!-- 列表 -->
    <el-table
      :data="menulist"
      border
      style="width: 100%"
      row-key="id"
      :tree-props="{ children: 'children' }"
      default-expand-all
    >
      <el-table-column prop="id" label="菜单编号" width="180">
      </el-table-column>
      <el-table-column prop="title" label="菜单名称" width="180">
      </el-table-column>
      <el-table-column prop="pid" label="上级菜单"> </el-table-column>
      <el-table-column label="菜单图标" width="180">
        <template slot-scope="item">
          <div>
            <i v-if="item.row.icon" :class="item.row.icon"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="菜单地址" width="180">
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary" size="mini" @click="edit(item.row.id)">
              编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(item.row.id)"
              circle
            >
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { delMenu } from "../../request/api";
//vuex中的辅助性函数
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
  mounted() {
    //页面一加载就调用接口
    // let res = await getMenuList();
    // console.log(res, "菜单列表");
    // console.log(this.$store,'仓库')
    //页面一加载就触发行动
    this.getMenu();
  },
  methods: {
    ...mapActions({
      getMenu: "menu/getMenuAction",
    }),
    //封装一个编辑事件 用于打开弹框
    edit(id) {
      this.$emit("edit", id);
    },
    //封装一个删除事件
    del(id) {
      console.log(id, "接收到的内容");
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          delMenu({
            id,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped></style>
